<template>
	<view class="home">
		<custom-navbar title="我的活动" :show-back="true" @back="handleBack" />

		<view class="content p30 pb150" :style="{ paddingTop: navBarHeight + 'px' }">
			<!-- 空状态 -->
			<view v-if="isEmpty" class="empty-state fcolcc">
				<image class="empty-image" src="/static/images/common/empty.png"></image>
				<view class="empty-text">暂无活动数据</view>
				<up-button type="primary" shape="circle" @click="loadActivityList(true)"
					style="width: 200rpx;margin-top: 30rpx;">
					重新加载
				</up-button>
			</view>

			<!-- 活动列表 -->
			<view v-else class="w100">
				<view v-for="(item,index) in activityList" :key="index" class="w100 mb20">
					<view class="cover">
						<image class="img" :src="item.activity_cover" mode="aspectFill"></image>
						<view class="top fcc">
							<image class="rentout" :src="cdn('仅出租.svg')"></image>
							<view class="text f22bc99A2E" :style="{ color: getStatusColor(item.activity_status_text) }">
								{{item.activity_status_text}}
							</view>
						</view>
					</view>
					<view class="bttoom p30">
						<view class="f30bc0 mb20">{{item.activity_title}}</view>
						<view class="f245c3">
							活动时间：{{formatDate(item.activity_start_time)}}-{{formatDate(item.activity_end_time)}}</view>
						<up-line margin='30rpx 0 30rpx 0'></up-line>
						<view class="fcsb">
							<view class="f245c9">
								活动地点：{{item.activity_address}}
							</view>
							<view class="btn f20bcf fcc" @click="handleDetails(item)">
								查看详情
							</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 加载更多 -->
			<view v-if="isLoadingMore" class="loading-more fcc">
				<up-loading-icon text='加载中...'></up-loading-icon>
			</view>
			<view v-if="noMoreData" class="no-more fcc">
				没有更多数据了
			</view>
		</view>
	</view>
</template>

<script setup>
	import CustomNavbar from '@/components/custom-navbar.vue'
	import {
		ref,
		onMounted,
		computed
	} from 'vue';
	import {
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		myActivity
	} from '@/api/user'

	// 系统信息
	const systemInfo = uni.getSystemInfoSync()
	const navBarHeight = computed(() => systemInfo.statusBarHeight + 44)

	// 数据状态
	const isLoading = ref(false)
	const isLoadingMore = ref(false)
	const noMoreData = ref(false)
	const isEmpty = ref(false)

	// 分页参数
	const page = ref(1)
	const limit = ref(10)
	const total = ref(0)

	// 活动列表数据
	const activityList = ref([])

	// 初始化加载
	onMounted(() => {
		loadActivityList(true)
	})

	// 加载活动列表
	const loadActivityList = async (isRefresh = false) => {
		if (isLoading.value && !isRefresh) return

		try {
			if (isRefresh) {
				page.value = 1
				noMoreData.value = false
				isLoading.value = true
			} else {
				isLoadingMore.value = true
			}

			const params = {
				page: page.value,
				limit: limit.value
			}

			const res = await myActivity(params)

			if (res.code === 200) {
				if (isRefresh) {
					activityList.value = res.data.data
					total.value = res.data.total
				} else {
					activityList.value = [...activityList.value, ...res.data.data]
				}

				// 检查是否还有更多数据
				if (res.data.data.length < limit.value) {
					noMoreData.value = true
				}

				// 检查空状态
				isEmpty.value = activityList.value.length === 0
			}
		} catch (error) {
			console.error('加载活动列表失败:', error)
			uni.showToast({
				title: '加载失败，请重试',
				icon: 'none'
			})
		} finally {
			isLoading.value = false
			isLoadingMore.value = false
		}
	}

	// 格式化日期
	const formatDate = (dateString) => {
		if (!dateString) return ''
		const date = new Date(dateString)
		return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`
	}

	// 获取状态颜色
	const getStatusColor = (status) => {
		switch (status) {
			case '待开始':
				return '#FF0C10'
			case '进行中':
				return '#398DFB'
			case '已完成':
				return '#999999'
			default:
				return '#333333'
		}
	}

	// 查看详情
	const handleDetails = (item) => {
		uni.navigateTo({
			url: `/pages/activity/apply?id=${item.activity_id}` // 假设item对象中有id字段
		});
	}

	// 返回
	const handleBack = () => {
		uni.navigateBack()
	}

	// 加载更多
	onReachBottom(() => {
		if (isLoadingMore.value || noMoreData.value) return
		page.value += 1
		loadActivityList()
	})
</script>

<style lang="scss" scoped>
	.home {
		width: 100%;
		min-height: 100vh;
		background-color: #f7f7f7;

		.content {
			padding-top: var(--status-bar-height);
			padding-top: calc(var(--status-bar-height) + 44px);

			.empty-state {
				height: 60vh;
				padding: 40rpx;
				text-align: center;

				.empty-image {
					width: 300rpx;
					height: 300rpx;
					margin-bottom: 30rpx;
				}

				.empty-text {
					font-size: 28rpx;
					color: #999;
				}
			}

			.cover {
				width: 100%;
				height: 320rpx;
				position: relative;
				border-radius: 20rpx 20rpx 0 0;
				overflow: hidden;

				.img {
					width: 100%;
					height: 100%;
				}

				.top {
					position: absolute;
					right: 30rpx;
					top: 0;
					flex-direction: column;

					.rentout {
						width: 100rpx;
						height: 94rpx;
					}

					.text {
						position: absolute;
						top: 44rpx;
						font-size: 22rpx;
						font-weight: bold;
					}
				}
			}

			.bttoom {
				background: #FFFFFF;
				border-radius: 0 0 20rpx 20rpx;

				.f30bc0 {
					font-size: 30rpx;
					font-weight: bold;
					color: #333;
				}

				.f245c3 {
					font-size: 24rpx;
					color: #5c5c5c;
				}

				.f245c9 {
					font-size: 24rpx;
					color: #5c5c5c;
				}

				.btn {
					width: 150rpx;
					height: 50rpx;
					background: linear-gradient(90deg, #398DFB 0%, #61DBFF 100%);
					border-radius: 10rpx;
					color: #FFFFFF;
				}
			}

			.loading-more,
			.no-more {
				padding: 30rpx 0;
				color: #999;
				font-size: 26rpx;
			}
		}
	}

	.fcolcc {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.fcsb {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.fcc {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.p30 {
		padding: 30rpx;
	}

	.pb150 {
		padding-bottom: 150rpx;
	}

	.mb20 {
		margin-bottom: 20rpx;
	}

	.w100 {
		width: 100%;
	}
</style>